# -*- coding: utf-8 -*-
"""
Created on Mon Jul 25 21:08:58 2016

@author: Yin Qiaonan
"""

from urllib.request import urlopen
from urllib.error import URLError
from bs4 import BeautifulSoup as BS

def getPM25(cityname):
    try:
        html = urlopen('http://www.pm25.com/city/' + cityname[0] + '.html')
    except URLError as e:
        print(e)
    else:
        pmObj = BS(html, 'lxml')
        html.close()
        try:
            pm25 = pmObj.find('ul', {'class' : 'pj_area_data_details'}).find('span', \
                {'class' : 'pjadt_pm25'})
        except AttributeError:
            return None
    try:
        html = urlopen('http://www.pm25.com/rank.html')
    except URLError as e:
        print(e)
    else:
        rnkObj = BS(html, 'lxml')
        html.close()
        try:
            allrank = rnkObj.find('ul', class_ = 'pj_area_data_details rank_box').find_all('li')
        except AttributeError:
            return None
        else:
            for cityrank in allrank:
                if cityrank.a.get_text() == cityname[1]:
                    return [pm25.get_text(), cityrank.find('span', {'class' : 'pjadt_ranknum'}).get_text()]

cities = [['nanjing', '南京'],['shanghai', '上海'], ['hangzhou', '杭州']]
for city in cities:
    pm25Info = getPM25(city)
    print(city[1] + ' PM2.5: ' + pm25Info[0] + ', 全国排名: ' + pm25Info[1])